#delimit;
clear all;
set maxvar 25000;
set matsize 2500;
set more off;
set emptycells drop;


#delimit cr


#delimit;
use "NORC_Data.dta";


do stata-tex;

cd "Output";


gen norc_weight_rep = .;
replace norc_weight_rep = norc_weight if rep_response == 1;
replace norc_weight_rep = 0 if rep_response == 0;

gen norc_weight_nonrep = .;
replace norc_weight_nonrep = norc_weight if rep_response == 0;
replace norc_weight_nonrep = 0 if rep_response == 1;

gen norc_weight_pt_rep  = .;
replace norc_weight_pt_rep = norc_weight if rep_pre_treatment == 1;
replace norc_weight_pt_rep = 0 if rep_pre_treatment == 0;

gen norc_weight_pt_nonrep  = .;
replace norc_weight_pt_nonrep = norc_weight if rep_pre_treatment == 0;
replace norc_weight_pt_nonrep = 0 if rep_pre_treatment == 1;


gen norc_weight_rep_exl = .;
replace norc_weight_rep_exl = norc_weight if rep_response_exl == 1;
replace norc_weight_rep_exl = 0 if rep_response_exl == 0;

gen norc_weight_nonrep_exl = .;
replace norc_weight_nonrep_exl = norc_weight if rep_response_exl == 0;
replace norc_weight_nonrep_exl = 0 if rep_response_exl == 1;



gen pid7 = partyid7-1;

*bysort partyid7: egen m_rep_cont = mean(rep_response) if treatment == 0;
*bysort partyid7: egen m_rep_treat = mean(rep_response) if treatment == 1;

*bysort partyid7: egen m_rep_exl_cont = mean(rep_response_exl) if treatment == 0;
*bysort partyid7: egen m_rep_exl_treat = mean(rep_response_exl) if treatment == 1;


*label variable m_rep_cont "Prob. Republican";
*label variable partyid7 "Baseline Seven-Point Partisanship";
label variable pid7 "Baseline Seven-Point Partisanship";

gen pid_treat_inter_pooled_1 = 1.pid7*treatment +2.pid7*treatment+3.pid7*treatment;
label variable pid_treat_inter_pooled_1 "Treatment $\times$ Baseline Dem.";

gen pid_treat_inter_pooled_2 = 4.pid7*treatment;
label variable pid_treat_inter_pooled_2 "Treatment $\times$ Baseline Ind.";

gen pid_treat_inter_pooled_3 = 5.pid7*treatment +6.pid7*treatment+7.pid7*treatment;
label variable pid_treat_inter_pooled_3 "Treatment $\times$ Baseline Rep.";


gen pid_treat_inter_pooled_1_exl = 1.pid7*treatment +2.pid7*treatment;
label variable pid_treat_inter_pooled_1_exl "Treatment $\times$ Baseline Dem. (Ex. Lean)";

gen pid_treat_inter_pooled_2_exl = 3.pid7*treatment+ 4.pid7*treatment + 5.pid7*treatment;
label variable pid_treat_inter_pooled_2_exl "Treatment $\times$ Baseline Ind. (Ex. Lean)";

gen pid_treat_inter_pooled_3_exl = 6.pid7*treatment+7.pid7*treatment;
label variable pid_treat_inter_pooled_3_exl "Treatment $\times$ Baseline Rep. (Ex. Lean)";


gen pid3 = 1 if pid7 <= 3;
replace pid3 = 2 if pid7 == 4;
replace pid3 = 3 if pid7 >= 5;

gen pid3_exl = 1 if pid7<= 2;
replace pid3_exl = 2 if pid7 >= 3 & pid7 <= 5;
replace pid3_exl = 3 if pid7>= 6;

gen nonbla = .;
replace nonbla = 0 if race_cat == 2;
replace nonbla = 1 if race_cat != 2;


gen under_55_yo = .;
replace under_55_yo = 1 if age7 <= 4;
replace under_55_yo = 0 if age7 >= 5; 

gen treat_nonbla = treatment*nonbla; 
gen treat_under_55_yo = treatment*under_55_yo; 

label variable nonbla "non-Black";
label variable under_55_yo "Under 55 Y.O.";
label variable treat_nonbla "Treatment $\times$ non-Black";
label variable treat_under_55_yo "Treatment $\times$ Under 55 Y.O.";

gen ind_response = .;
replace ind_response = 1 if dem_response == 0 & rep_response == 0;
replace ind_response = 0 if dem_response == 1 | rep_response == 1;

gen ind_response_exl = .;
replace ind_response_exl = 1 if dem_response_exl == 0 & rep_response_exl == 0;
replace ind_response_exl = 0 if dem_response_exl == 1 | rep_response_exl == 1;

gen partyid5_survey = .;
replace partyid5_survey = 1 if dem_response_exl  == 1;
replace partyid5_survey = 2 if dem_response_exl  == 0 & dem_response == 1;
replace partyid5_survey = 3 if ind_response == 1;
replace partyid5_survey = 4 if rep_response_exl  == 0 & rep_response == 1;
replace partyid5_survey = 5 if rep_response_exl  == 1;


label variable rep_response "Republican";
label variable treatment "Treatment";
label variable rep_response_exl "Republican (Excluding Leaners)";
label variable rep_pre_treatment "Republican (Baseline)";








local covariates i.female i.age_4_cat i.educ_4_cat i.race_cat i.hh_inc_5_cat i.census_4_cat i.employment_status i.survey_mode;


*Testing for Non-Response Differences across Treatment and Control;
gen rep_nonresponse = 0 if rep_response != .;
replace rep_nonresponse = 1 if rep_response == .;

gen app_nonresponse = 0 if app_response != .;
replace app_nonresponse = 1 if app_response == .;

tabulate rep_nonresponse treatment, chi2;
tabulate app_nonresponse treatment, chi2;


eststo: reg rep_nonresponse treatment, robust;
estadd local weighted "No";
estadd local covs "No";

eststo: reg rep_nonresponse treatment [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "No";

eststo: reg rep_nonresponse treatment `covariates', robust;
estadd local weighted "No";
estadd local covs "Yes";

eststo: reg rep_nonresponse treatment `covariates' [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "Yes";



eststo: reg app_nonresponse treatment, robust;
estadd local weighted "No";
estadd local covs "No";

eststo: reg app_nonresponse treatment [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "No";

eststo: reg app_nonresponse treatment `covariates', robust;

estadd local weighted "No";
estadd local covs "Yes";

eststo: reg app_nonresponse treatment `covariates' [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "Yes";


esttab using NonResponse_Outcome.tex, note("Two-sided p-values calculated from heteroskedasticity robust standard errors reported in parentheses")  keep(treatment)
label p star(+ 0.10 * 0.05 ** 0.01 *** 0.001) mlabels("Part." "Part." "Part." "Part." "App." "App." "App." "App.") r2 scalars("weighted Weighted" "covs Covariates")
title("Probability Respondent Does Not Respond to Item \label{NonResponseOutcome}") replace;
eststo clear;




*Prob. Republican Analysis;
eststo: reg rep_response treatment, robust;
estadd local weighted "No";
estadd local covs "No";

eststo: reg rep_response treatment [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "No";

eststo: reg rep_response treatment i.wave_cat `covariates', robust;
estadd local weighted "No";
estadd local covs "Yes";

eststo: reg rep_response treatment i.wave_cat `covariates' [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "Yes";


*Excluding_Leaners;
eststo: reg rep_response_exl treatment, robust;
estadd local weighted "No";
estadd local covs "No";

eststo: reg rep_response_exl treatment [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "No";

eststo: reg rep_response_exl treatment i.wave_cat `covariates', robust;
estadd local weighted "No";
estadd local covs "Yes";

eststo: reg rep_response_exl treatment i.wave_cat `covariates' [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "Yes";


esttab using Rep_Outcome.tex, note("Two-sided p-values calculated from heteroskedasticity robust standard errors reported in parentheses")  keep(treatment)
label p star(+ 0.10 * 0.05 ** 0.01 *** 0.001) mlabels("Lean" "Lean" "Lean" "Lean" "Ex. Lean" "Ex. Lean" "Ex. Lean" "Ex. Lean") r2 scalars("weighted Weighted" "covs Covariates")
title("Probability Respondent Identifies as Republican \label{RepOutcome}") replace;
eststo clear;


eststo: reg rep_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3, robust;
estadd local weighted "No";
estadd local covs "No";
estadd local pid3_FE "Yes";

eststo: reg rep_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3  [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "No";
estadd local pid3_FE "Yes";

eststo: reg rep_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3 i.wave_cat `covariates' , robust;
estadd local weighted "No";
estadd local covs "Yes";
estadd local pid3_FE "Yes";

eststo: reg rep_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3 i.wave_cat `covariates' [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "Yes";
estadd local pid3_FE "Yes";


eststo: reg ind_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3, robust;
estadd local weighted "No";
estadd local covs "No";
estadd local pid3_FE "Yes";

eststo: reg ind_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3  [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "No";
estadd local pid3_FE "Yes";

eststo: reg ind_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3 i.wave_cat `covariates' , robust;
estadd local weighted "No";
estadd local covs "Yes";
estadd local pid3_FE "Yes";

eststo: reg ind_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3 i.wave_cat `covariates' [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "Yes";
estadd local pid3_FE "Yes";


esttab using Rep_Outcome_Het_PID7_Pooled.tex, note("Two-sided p-values calculated from heteroskedasticity robust standard errors reported in parentheses")  
keep( pid_treat_inter_pooled_1 pid_treat_inter_pooled_2 pid_treat_inter_pooled_3 )
label p star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  mlabels("Rep" "Rep" "Rep" "Rep" "Ind" "Ind" "Ind" "Ind") r2 scalars("weighted Weighted" "covs Covariates"
"pid3_FE 3-Point PID FEs")
title("Probability Respondent Identifies with Party Allowing Treatment Effect to Vary Across Baseline Three-Point Partisanship \label{RepOutcomeHetPID7Pooled}") replace;
eststo clear; 




eststo: reg rep_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3 if b_partyq_post2018  != 0, robust;
estadd local weighted "No";
estadd local covs "No";
estadd local pid3_FE "Yes";

eststo: reg rep_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3  [pweight=norc_weight] if b_partyq_post2018  != 0, robust;
estadd local weighted "Yes";
estadd local covs "No";
estadd local pid3_FE "Yes";

eststo: reg rep_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3 i.wave_cat `covariates' if b_partyq_post2018  != 0, robust;
estadd local weighted "No";
estadd local covs "Yes";
estadd local pid3_FE "Yes";

eststo: reg rep_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3 i.wave_cat `covariates' [pweight=norc_weight] if b_partyq_post2018  != 0, robust;
estadd local weighted "Yes";
estadd local covs "Yes";
estadd local pid3_FE "Yes";


eststo: reg ind_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3 if b_partyq_post2018  != 0, robust;
estadd local weighted "No";
estadd local covs "No";
estadd local pid3_FE "Yes";

eststo: reg ind_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3  [pweight=norc_weight] if b_partyq_post2018  != 0, robust;
estadd local weighted "Yes";
estadd local covs "No";
estadd local pid3_FE "Yes";

eststo: reg ind_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3 i.wave_cat `covariates' if b_partyq_post2018  != 0, robust;
estadd local weighted "No";
estadd local covs "Yes";
estadd local pid3_FE "Yes";

eststo: reg ind_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3 i.wave_cat `covariates' [pweight=norc_weight] if b_partyq_post2018  != 0, robust;
estadd local weighted "Yes";
estadd local covs "Yes";
estadd local pid3_FE "Yes";


esttab using Rep_Outcome_Het_PID7_Pooled_Post2018Robust.tex, note("Two-sided p-values calculated from heteroskedasticity robust standard errors reported in parentheses")  
keep( pid_treat_inter_pooled_1 pid_treat_inter_pooled_2 pid_treat_inter_pooled_3 )
label p star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  mlabels("Rep" "Rep" "Rep" "Rep" "Ind" "Ind" "Ind" "Ind") r2 scalars("weighted Weighted" "covs Covariates"
"pid3_FE 3-Point PID FEs")
title("Probability Respondent Identifies with Party Allowing Treatment Effect to Vary Across Baseline Three-Point Partisanship \label{RepOutcomeHetPID7PooledPost2018Robust}") replace;
eststo clear; 



*Prob. Approve Analysis;
eststo: reg app_response treatment, robust;
estadd local weighted "No";
estadd local covs "No";

eststo: reg app_response treatment [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "No";

eststo: reg app_response treatment i.wave_cat `covariates' , robust;
estadd local weighted "No";
estadd local covs "Yes";

eststo: reg app_response treatment i.wave_cat `covariates' [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "Yes";


esttab using App_Outcome.tex, note("Two-sided p-values calculated from heteroskedasticity robust standard errors reported in parentheses")  keep(treatment)
label p star(+ 0.10 * 0.05 ** 0.01 *** 0.001) mlabels("" "" "" "") r2 scalars("weighted Weighted" "covs Covariates")
title("Probability Respondent Approves of President \label{AppOutcome}") replace;
eststo clear;

*Prob. Approve Conditional on Republican Analysis;
eststo: reg app_response treatment if rep_response == 1, robust;
estadd local weighted "No";
estadd local covs "No";

eststo: reg app_response treatment [pweight=norc_weight_rep] if rep_response == 1, robust;
estadd local weighted "Yes";
estadd local covs "No";

eststo: reg app_response treatment i.wave_cat `covariates' if rep_response == 1, robust;
estadd local weighted "No";
estadd local covs "Yes";

eststo: reg app_response treatment i.wave_cat `covariates' [pweight=norc_weight_rep] if rep_response == 1, robust;
estadd local weighted "Yes";
estadd local covs "Yes";

esttab using App_Outcome_Rep.tex, note("Two-sided p-values calculated from heteroskedasticity robust standard errors reported in parentheses")  keep(treatment)
label p star(+ 0.10 * 0.05 ** 0.01 *** 0.001) mlabels("" "" "" "") r2 scalars("weighted Weighted" "covs Covariates")
title("Probability Respondent Approves of President Conditional on Republican \label{AppOutcomeRep}") replace;
eststo clear;




eststo: reg rep_response_exl pid_treat_inter_pooled_1_exl pid_treat_inter_pooled_2_exl pid_treat_inter_pooled_3_exl i.pid3_exl, robust;
estadd local weighted "No";
estadd local covs "No";
estadd local pid3_FE "Yes";

eststo: reg rep_response_exl pid_treat_inter_pooled_1_exl pid_treat_inter_pooled_2_exl pid_treat_inter_pooled_3_exl i.pid3_exl  [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "No";
estadd local pid3_FE "Yes";

eststo: reg rep_response_exl pid_treat_inter_pooled_1_exl pid_treat_inter_pooled_2_exl pid_treat_inter_pooled_3_exl i.pid3_exl i.wave_cat `covariates' , robust;
estadd local weighted "No";
estadd local covs "Yes";
estadd local pid3_FE "Yes";

eststo: reg rep_response_exl pid_treat_inter_pooled_1_exl pid_treat_inter_pooled_2_exl pid_treat_inter_pooled_3_exl i.pid3_exl i.wave_cat `covariates' [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "Yes";
estadd local pid3_FE "Yes";



esttab using Rep_Outcome_ExL.tex, note("Two-sided p-values calculated from heteroskedasticity robust standard errors reported in parentheses")  keep(pid_treat_inter_pooled_1_exl pid_treat_inter_pooled_2_exl pid_treat_inter_pooled_3_exl)
label p star(+ 0.10 * 0.05 ** 0.01 *** 0.001) mlabels("" "" "" "" "" "" "" "") r2 scalars("weighted Weighted" "covs Covariates" "pid3_FE 3-Point PID FEs")
title("Probability Respondent Identifies as Republican Excluding Leaners \label{RepOutcomeExL}") replace;
eststo clear;








eststo: reg rep_response treat_nonbla treatment nonbla, robust;
estadd local weighted "No";
estadd local covs "No";

lincom treat_nonbla + treatment;

eststo: reg rep_response treat_nonbla treatment nonbla  [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "No";

lincom treat_nonbla + treatment;
eststo: reg rep_response treat_nonbla treatment nonbla  `covariates' , robust;
estadd local weighted "No";
estadd local covs "Yes";

lincom treat_nonbla + treatment;
eststo: reg rep_response treat_nonbla treatment nonbla  `covariates' [pweight=norc_weight], robust;
lincom treat_nonbla + treatment;
estadd local weighted "Yes";
estadd local covs "Yes";


eststo: reg rep_response treat_under_55_yo  treatment under_55_yo , robust;
lincom treat_under_55_yo + treatment;
estadd local weighted "No";
estadd local covs "No";

eststo: reg rep_response treat_under_55_yo  treatment under_55_yo  [pweight=norc_weight], robust;
lincom treat_under_55_yo + treatment;
estadd local weighted "Yes";
estadd local covs "No";

eststo: reg rep_response treat_under_55_yo  treatment under_55_yo  `covariates', robust;
lincom treat_under_55_yo + treatment ;
estadd local weighted "No";
estadd local covs "Yes";

eststo: reg rep_response treat_under_55_yo  treatment under_55_yo  `covariates' [pweight=norc_weight], robust;
lincom treat_under_55_yo + treatment;
estadd local weighted "Yes";
estadd local covs "Yes";

esttab using Rep_Outcome_Het_Demog.tex, note("Two-sided p-values calculated from heteroskedasticity robust standard errors reported in parentheses")  
keep( treat_nonbla treat_under_55_yo treatment nonbla under_55_yo) order( treat_nonbla treat_under_55_yo treatment nonbla under_55_yo)
label p star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  mlabels("Rep" "Rep" "Rep" "Rep" "Rep" "Rep" "Rep" "Rep") r2 scalars("weighted Weighted" "covs Covariates")
title("Probability Respondent Identifies with Party Allowing Treatment Effect to Vary Across Demographics \label{RepOutcomeHetDemog}") replace;
eststo clear; 









eststo: logit rep_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3, robust;
estadd local weighted "No";
estadd local covs "No";
estadd local pid_3FE "Yes";

eststo: logit rep_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3  [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "No";
estadd local pid_3FE "Yes";

eststo: logit rep_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3 i.wave_cat `covariates' , robust;
estadd local weighted "No";
estadd local covs "Yes";
estadd local pid_3FE "Yes";

eststo: logit rep_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3 i.wave_cat `covariates' [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "Yes";
estadd local pid_3FE "Yes";


eststo: logit ind_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3, robust;
estadd local weighted "No";
estadd local covs "No";
estadd local pid_3FE "Yes";

eststo: logit ind_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3  [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "No";
estadd local pid_3FE "Yes";

eststo: logit ind_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3 i.wave_cat `covariates' , robust;
estadd local weighted "No";
estadd local covs "Yes";
estadd local pid_3FE "Yes";

eststo: logit ind_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid3 i.wave_cat `covariates' [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "Yes";
estadd local pid_3FE "Yes";


esttab using Rep_Outcome_Het_PID7_Pooled_Logit.tex, note("Two-sided p-values calculated from heteroskedasticity robust standard errors reported in parentheses")  
keep( pid_treat_inter_pooled_1 pid_treat_inter_pooled_2 pid_treat_inter_pooled_3 )
label p star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  mlabels("Rep" "Rep" "Rep" "Rep" "Ind" "Ind" "Ind" "Ind") aic 
scalars("weighted Weighted" "covs Covariates" "pid_3FE 3-Point PID FEs")
title("Logit Models Respondent Identifies with Party Allowing Treatment Effect to Vary Across Baseline Three-Point Partisanship \label{RepOutcomeHetPID7PooledLogit}") replace;
eststo clear; 





#delimit cr

forvalues k=1(1)7{

gen pid_treat_inter_`k' = `k'.pid7*treatment 
label variable pid_treat_inter_`k' "Treatment $\times$ PID=`k'"

}


#delimit;
eststo: reg rep_response pid_treat_inter_1-pid_treat_inter_7 i.pid7, robust;
estadd local weighted "No";
estadd local covs "No";
estadd local pid7_FE "Yes";

eststo: reg rep_response pid_treat_inter_1-pid_treat_inter_7 i.pid7  [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "No";
estadd local pid7_FE "Yes";

eststo: reg rep_response pid_treat_inter_1-pid_treat_inter_7 i.pid7 i.wave_cat `covariates' , robust;
estadd local weighted "No";
estadd local covs "Yes";
estadd local pid7_FE "Yes";

eststo: reg rep_response pid_treat_inter_1-pid_treat_inter_7 i.pid7 i.wave_cat `covariates' [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "Yes";
estadd local pid7_FE "Yes";


eststo: reg rep_response_exl pid_treat_inter_1-pid_treat_inter_7 i.pid7, robust;
estadd local weighted "No";
estadd local covs "No";
estadd local pid7_FE "Yes";

eststo: reg rep_response_exl pid_treat_inter_1-pid_treat_inter_7 i.pid7  [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "No";
estadd local pid7_FE "Yes";

eststo: reg rep_response_exl pid_treat_inter_1-pid_treat_inter_7 i.pid7 i.wave_cat `covariates' , robust;
estadd local weighted "No";
estadd local covs "Yes";
estadd local pid7_FE "Yes";

eststo: reg rep_response_exl pid_treat_inter_1-pid_treat_inter_7 i.pid7 i.wave_cat `covariates' [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "Yes";
estadd local pid7_FE "Yes";


esttab using Rep_Outcome_Het_PID7.tex, note("Two-sided p-values calculated from heteroskedasticity robust standard errors reported in parentheses")  
keep( pid_treat_inter_1 pid_treat_inter_2 pid_treat_inter_3 pid_treat_inter_4 pid_treat_inter_5 pid_treat_inter_6 pid_treat_inter_7)
label p star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  mlabels("Lean" "Lean" "Lean" "Lean" "Ex. Lean" "Ex. Lean" "Ex. Lean" "Ex. Lean" ) r2 scalars("weighted Weighted" "covs Covariates" )
title("Probability Respondent Identifies as Republican Allowing Treatment Effect to Vary Across Baseline Seven-Point Partisanship \label{RepOutcomeHetPID7}") replace;
eststo clear;




eststo: reg rep_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid7, robust;
estadd local weighted "No";
estadd local covs "No";
estadd local pid7_FE "Yes";

eststo: reg rep_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid7  [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "No";
estadd local pid7_FE "Yes";

eststo: reg rep_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid7 i.wave_cat `covariates' , robust;
estadd local weighted "No";
estadd local covs "Yes";
estadd local pid7_FE "Yes";

eststo: reg rep_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid7 i.wave_cat `covariates' [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "Yes";
estadd local pid7_FE "Yes";


eststo: reg ind_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid7, robust;
estadd local weighted "No";
estadd local covs "No";
estadd local pid7_FE "Yes";

eststo: reg ind_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid7  [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "No";
estadd local pid7_FE "Yes";

eststo: reg ind_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid7 i.wave_cat `covariates' , robust;
estadd local weighted "No";
estadd local covs "Yes";
estadd local pid7_FE "Yes";

eststo: reg ind_response pid_treat_inter_pooled_1-pid_treat_inter_pooled_3 i.pid7 i.wave_cat `covariates' [pweight=norc_weight], robust;
estadd local weighted "Yes";
estadd local covs "Yes";
estadd local pid7_FE "Yes";


esttab using Rep_Outcome_Het_PID7_Pooled_Robust.tex, note("Two-sided p-values calculated from heteroskedasticity robust standard errors reported in parentheses")  
keep( pid_treat_inter_pooled_1 pid_treat_inter_pooled_2 pid_treat_inter_pooled_3 )
label p star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  mlabels("Rep" "Rep" "Rep" "Rep" "Ind" "Ind" "Ind" "Ind") r2 scalars("weighted Weighted" "covs Covariates" "pid7_FE 7-Point PID FEs")
title("Probability Respondent Identifies as Republican Allowing Treatment Effect to Vary Across Baseline Three-Point Partisanship with Seven-Point Partisanship FEs\label{RepOutcomeHetPID7PooledRobust}") replace;

eststo clear;





#delimit;
gen resp_id = _n;


export delimited resp_id app_response treatment pid7 norc_weight wave_cat female age_4_cat educ_4_cat race_cat hh_inc_5_cat census_4_cat employment_status survey_mode using "rep_baseline_samp.csv" if  pid7 == 5 | pid7 == 6 | pid7 == 7, replace;
export delimited resp_id app_response treatment pid7 norc_weight wave_cat female age_4_cat educ_4_cat race_cat hh_inc_5_cat census_4_cat employment_status survey_mode using "rep_survey_samp.csv" if rep_response == 1, replace;

export delimited resp_id app_response treatment pid7 norc_weight wave_cat female age_4_cat educ_4_cat race_cat hh_inc_5_cat census_4_cat employment_status survey_mode using "rep_baseline_samp_exl.csv" if pid7 == 6 | pid7 == 7, replace;
export delimited resp_id app_response treatment pid7 norc_weight wave_cat female age_4_cat educ_4_cat race_cat hh_inc_5_cat census_4_cat employment_status survey_mode using "rep_survey_samp_exl.csv" if rep_response_exl == 1, replace;

#delimit;
set more off;
clear all;

insheet using "rep_baseline_samp.csv";
gen rep_baseline_samp = 1;
save "rep_baseline_samp.dta", replace;


#delimit;
clear all;

cd "../";
do stata-tex;


#delimit cr
capture progam drop get_star_coeff
program define get_star_coeff, rclass
	args p_input est_input model_num
	
	if `p_input' >= 0.1 {
		local estout_`model_num' = string(round(`est_input', .0001))

	}
	else if `p_input' < 0.1 & `p_input' >= 0.05 {
		local estout_`model_num' = string(round(`est_input', .0001)) + "$^+$"
	} 
	else if `p_input' < 0.05 & `p_input' >= 0.01 {
		local estout_`model_num' = string(round(`est_input', .0001)) + "\sym{*}"
	} 
	else if `p_input' < 0.01 & `p_input' >= 0.001 {
		local estout_`model_num' = string(round(`est_input', .0001)) + "\sym{**}"	
		
	} 
	else {
		local estout_`model_num' = string(round(`est_input', .0001)) + "\sym{***}"
	}
	
	return local estout_star_`model_num' `"`estout_`model_num''"'

end

#delimit;
insheet using "Output/rep_survey_samp.csv";
gen rep_survey_samp = 1;

append using "Output/rep_baseline_samp.dta";
replace rep_survey_samp = 0 if rep_baseline_samp == 1;
replace rep_baseline_samp = 0 if rep_survey_samp == 1;

label var rep_survey_samp "Republican In Survey";
label var rep_baseline_samp "Republican In Baseline";

local covariates i.female i.age_4_cat i.educ_4_cat i.race_cat i.hh_inc_5_cat i.census_4_cat i.employment_status i.survey_mode;

reg app_response i.rep_survey_samp, robust cluster(resp_id);
local est_1 = _b[1.rep_survey_samp];
local se_1 = _se[1.rep_survey_samp];
local p_1 = (2 * ttail(e(df_r), abs(`est_1'/`se_1')));
get_star_coeff `p_1' `est_1' 1;
local estout_1  = r(estout_star_1) ;

insert_into_file using sample_table.csv, key(estout_1) value(`estout_1');
insert_into_file using sample_table.csv, key(p_1) value(`p_1') format(%5.4f);
insert_into_file using sample_table.csv, key(N_1) value(`e(N)') format(%5.0f);
insert_into_file using sample_table.csv, key(R2_1) value(`e(r2)') format(%5.4f);

margins i.rep_survey_samp, post;
matrix test = e(b);
local mean_baseline_1 = test[1,1];
insert_into_file using sample_table.csv, key(mean_baseline_1) value(`mean_baseline_1') format(%5.4f);

reg app_response i.rep_survey_samp [pweight = norc_weight], robust cluster(resp_id);
local est_2 = _b[1.rep_survey_samp];
local se_2 = _se[1.rep_survey_samp];
local p_2 = (2 * ttail(e(df_r), abs(`est_2'/`se_2')));
get_star_coeff `p_2' `est_2' 2;
local estout_2  = r(estout_star_2) ;

insert_into_file using sample_table.csv, key(estout_2) value(`estout_2');
insert_into_file using sample_table.csv, key(p_2) value(`p_2') format(%5.4f);
insert_into_file using sample_table.csv, key(N_2) value(`e(N)') format(%5.0f);
insert_into_file using sample_table.csv, key(R2_2) value(`e(r2)') format(%5.4f);

margins i.rep_survey_samp, post;
matrix test = e(b);
local mean_baseline_2 = test[1,1];
insert_into_file using sample_table.csv, key(mean_baseline_2) value(`mean_baseline_2') format(%5.4f);


reg app_response i.rep_survey_samp i.wave_cat `covariates', robust cluster(resp_id);
local est_3 = _b[1.rep_survey_samp];
local se_3 = _se[1.rep_survey_samp];
local p_3 = (2 * ttail(e(df_r), abs(`est_3'/`se_3')));
get_star_coeff `p_3' `est_3' 3;
local estout_3  = r(estout_star_3) ;

insert_into_file using sample_table.csv, key(estout_3) value(`estout_3');
insert_into_file using sample_table.csv, key(p_3) value(`p_3') format(%5.4f);
insert_into_file using sample_table.csv, key(N_3) value(`e(N)') format(%5.0f);
insert_into_file using sample_table.csv, key(R2_3) value(`e(r2)') format(%5.4f);

margins i.rep_survey_samp, post;
matrix test = e(b);
local mean_baseline_3 = test[1,1];
insert_into_file using sample_table.csv, key(mean_baseline_3) value(`mean_baseline_3') format(%5.4f);


reg app_response i.rep_survey_samp i.wave_cat `covariates' [pweight = norc_weight], robust cluster(resp_id);
local est_4 = _b[1.rep_survey_samp];
local se_4 = _se[1.rep_survey_samp];
local p_4 = (2 * ttail(e(df_r), abs(`est_4'/`se_4')));
get_star_coeff `p_4' `est_4' 4;
local estout_4  = r(estout_star_4) ;

insert_into_file using sample_table.csv, key(estout_4) value(`estout_4');
insert_into_file using sample_table.csv, key(p_4) value(`p_4') format(%5.4f);
insert_into_file using sample_table.csv, key(N_4) value(`e(N)') format(%5.0f);
insert_into_file using sample_table.csv, key(R2_4) value(`e(r2)') format(%5.4f);


margins i.rep_survey_samp, post;
matrix test = e(b);
local mean_baseline_4 = test[1,1];
insert_into_file using sample_table.csv, key(mean_baseline_4) value(`mean_baseline_4') format(%5.4f);

table_from_tpl, t(template_app_outcome_across_rep_def.tex) r(sample_table.csv) o(Output/App_Outcome_Rep_Baseline_Survey.tex);

rm sample_table.csv; 
